<template>
  <el-dialog :close-on-click-modal="false" :title="form.id ? '编辑' : '新增'" :visible="show" :before-close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="事件来源" prop="sourceType">
        <SelectEventSourceType v-model="form.sourceType" @change="(e) => (form.sourceName = e.label)"></SelectEventSourceType>
      </el-form-item>
      <el-form-item label="事件类型" prop="eventLevel">
        <!-- <SelectEventType v-model="form.eventLevel" :arrVal="eventArr" style="width:100%" @change="onChangeEventType"></SelectEventType> -->
        <el-select v-model="form.category" placeholder="请选择" @change="onChangeEventType($event, 'quesOptions', problemOptions)">
          <el-option v-for="item in problemOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" :prop="isShowQues ? `type`:''">
        <el-select v-model="form.type" placeholder="请选择" style="width:100%;" @change="onChangeProblem($event, 'detailOptons', quesOptions)">
          <el-option v-for="item in quesOptions" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细类型" :prop="isShowQues?`subType`:''">
        <el-select v-model="form.subType" placeholder="请选择"  style="width:100%;" @change="onChangeProblem($event, null, detailOptons)">
          <el-option v-for="item in detailOptons" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="事件描述" prop="content">
        <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="pics">
        <ImageUpload v-model="form.pics"></ImageUpload>
      </el-form-item>
      <el-form-item label="详细地址" prop="address"><el-input v-model="form.address" type="text" placeholder="请输入"></el-input></el-form-item>
      <el-form-item label="坐标" prop="location">
        <el-input v-model="form.location" disabled type="text" placeholder="请在地图上打点"></el-input>
        <MapMarker ref="MapMarker" :isClick="true" @change="onChangeMap" style="margin-top:10px;"></MapMarker>
      </el-form-item>
      <el-form-item label="选择路线" prop="routeId">
            <SelectRoadLine ref="SelectRoadLineRef" v-model="form.routeId" @change="onChangeRoadLine"></SelectRoadLine>
            <!-- <SelectByApi v-model="form.routeId" :api="`/workbench/attend-route/nearby`" :params="{pageSize:999}" :filterable="true"  ></SelectByApi> -->
      </el-form-item>
      <!-- <el-form-item label="处理部门" prop="handleDeptCode">
        <SelectGroup v-model="form.handleDeptCode" :group-num="3" @change="onChangeGroup"></SelectGroup>
      </el-form-item> -->

      <el-form-item label="要求完成" prop="reqHandleTime">
        <el-input-number :disabled="isDisabled" v-model="form.reqHandleTime" placeholder="请输入"></el-input-number>
      </el-form-item>
      <el-form-item label="选择单位" prop="reqHandleTimeUnit">
        <el-select :disabled="isDisabled" v-model="form.reqHandleTimeUnit" placeholder="请选择单位">
          <el-option v-for="item in [{label: '天', value: '天'},{label: '小时', value: '小时'},{label: '分钟', value: '分钟'}]" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>

      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import MapMarker from "@/components/MapMarker";
import SelectRoadLine from './selectRoadLine.vue'
import { getTypeTree } from "@/api/eventType";
import { subForm } from '@/api/flow'
import { roadList } from "@/api/checkRoad";
export default {
  components: { MapMarker, SelectRoadLine },
  props: {
    /* 显示隐藏 */
    show: {
      type: Boolean,
      default: false,
    },
    item: {
      type: Object,
      default: {},
    },
  },
  watch: {
    item: {
      handler(val) {
        this.form = val || { reqHandleTimeUnit: "天" };
        if(this.form.eventLevel) {
          this.eventArr = [this.form.eventLevel, this.form.problemType, this.form.detailType]
        }
      },
      immediate: true,
    },
    show: {
      handler(val) {
        this.getTypeTree()
        val && this.resetForm("form");
        this.$nextTick((_) => {
          if (val) {
            this.$refs["MapMarker"].initMap();
          } else {
            this.$refs["MapMarker"] && this.$refs["MapMarker"].clearMarker();
          }
        });
      },
      immediate: true,
    },
  },
  computed: {
    isDisabled() {
      return this.isShowQues && this.form.problemType != "";
    },
    isShowQues() {
      const reg = /^JSWNJHJWSB[A-Z]*/;
      return reg.test(this.form.handleDeptCode);
      // return `JSWNJHJWSB`.includes(this.form.handleDeptCode)
    },
  },
  data() {
    return {
      form: {},
      eventArr: [],
      rules: {
        sourceType: [
          {
            required: true,
            message: "请选择来源",
            trigger: ["change", "input"],
          },
        ],
        category: [
          {
            required: true,
            message: "请选择类型",
            trigger: ["change", "input"],
          },
        ],
        handleDeptCode: [
          {
            required: true,
            message: "请选择部门",
            trigger: ["change", "input"],
          },
        ],
        content: { required: true, message: "请输入内容", trigger: ["blur"] },
        address: { required: true, message: "请输入地址", trigger: ["blur"] },
        routeId: { required: true, message: "请选择路线", trigger: ["change"] },
        location: {
          required: true,
          message: "请选择坐标",
          trigger: ["change", "input"],
        },
        reqHandleTime: {
          required: true,
          message: "请输入内容",
          trigger: ["change", "input"],
        },
        reqHandleTimeUnit: {
          required: true,
          message: "请选择单位",
          trigger: ["change", "input"],
        },
        type: {
          required: true,
          message: "请选择问题类型",
          trigger: ["change"],
        },
        subType: {
          required: true,
          message: "请选择详细类型",
          trigger: ["change"],
        },
      },
      quesOptions: [],
      detailOptons:[],
      problemOptions: [],
    };
  },
  methods: {
    roadList,
    submit() {
      console.log("打印当前编辑：", this.form);
        this.$set(this.form, "ARG_DEFINE_CODE", 'work');
        this.$set(this.form, "ARG_DEFINE_START_NODE", 'start');
        this.$set(this.form, "ARG_USER_SYSTEM", 1);
        this.$refs["form"].validate((valid) => {
            if (valid) {
                const gmtTimeLimit = this.calculateDate(this.form.reqHandleTime, this.form.reqHandleTimeUnit)
                this.$set(this.form, "gmtTimeLimit", gmtTimeLimit);
            if (this.form.id) {

            } else {
                subForm({...this.form, handleType: '1'}).then((res) => {
                // 验证成功-新增
                this.$emit("change", this.form);
                this.close();
                });
            }
        }
      });
    },
    calculateDate(num, unit) {
        if(!num || !unit) return null;
        let millisecond=0;
        if(unit == '天') {
            millisecond = num * 24 * 60 * 60 * 1000;
        }
        if(unit == '小时') {
            millisecond = num * 60 * 60 * 1000;
        }
        if(unit == '分钟'){
            millisecond = num * 60 * 1000;
        }
        return this.parseTime(new Date(+new Date() + millisecond));
    },
    onChangeMap(location) {
      this.$set(this.form, "location", location);
      this.$refs['SelectRoadLineRef'].search(location)
    },
    close() {
      this.$emit("update:show", false);
      this.form = {};
    },
    onChangeGroup(e) {
      console.log(this.form.handleDeptCode);
      this.$set(this.form, "handleDept", e.name);
    },
    onSelectQuesChange(data) {
      this.options = data.list;
      this.$set(this.form, "reqHandleTime", data.time);
      this.$set(this.form, "reqHandleTimeUnit", data.unit);
    },
    async getTypeTree() {
      try {
        const res = await getTypeTree({ id: 0 });
        this.problemOptions = res.data;
      } catch (error) {}
    },
    onChangeEventType(e, optionField, optionsList) {
      const item = optionsList.find(i => i.id == e)
      this[optionField] = item.children;
      this.$set(this.form, "reqHandleTime", item.time);
      this.$set(this.form, "reqHandleTimeUnit", item.unit);
      this.$set(this.form, "content", item.desc);
      this.$set(this.form, "categoryName", item.name);
      this.$set(this.form, "type", '');
      this.$set(this.form, "typeName", '');
      this.$set(this.form, "subType", '');
      this.$set(this.form, "subTypeName", '');
    },
    onChangeProblem(e, optionField, optionsList) {
      const item = optionsList.find(i => i.id == e)
      if(optionField) {
        this[optionField] = item.children;
        this.$set(this.form, "typeName", item.name);
        this.$set(this.form, "subType", '');
        this.$set(this.form, "subTypeName", '');
      }else{
        this.$set(this.form, "subTypeName", item.name);
      }
      this.$set(this.form, "reqHandleTime", item.time);
      this.$set(this.form, "reqHandleTimeUnit", item.unit);
      this.$set(this.form, "content", item.desc);
    },
    onChangeRoadLine(e) {
        this.$set(this.form, "routeName", e.name);
        // this.$refs["MapMarker"].createLayers(e)
    }
  },
};
</script>

<style lang="scss" scoped></style>
